package com.meeting.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.Page;
import com.meeting.dto.OrdersQueryDTO;
import com.meeting.entity.Orders;
import com.meeting.entity.Rooms;
import com.meeting.vo.OrdersVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

@Mapper
public interface OrdersMapper extends BaseMapper<Orders> {

//    @Select("select room_id from orders where start_time <= #{startTime} and end_time >= #{endTime} and status in (0,1,2)")
//    List<Integer> selectByStartAndEndTime(LocalDateTime startTime, LocalDateTime endTime);

    //  添加两个重载方法的声明，但不添加@Select注解，让实现在XML中完成
    List<Integer> selectByStartAndEndTime(@Param("startTime") LocalDateTime startTime, @Param("endTime") LocalDateTime endTime);
    List<Integer> selectByStartAndEndTime(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select("select * from orders where status = #{status} and sale_time < #{orderTime} ")
    List<Orders> selectByStatusAndOrderTimeLT(Integer status, LocalDateTime orderTime);

    Page<OrdersVO> listAll(OrdersQueryDTO ordersQueryDTO);

    List<Orders> selectOrdersInTimeRange(LocalDateTime start, LocalDateTime end);

    @Select("select * from orders where room_id = #{roomId} and start_time = #{startTime}")
    Orders selectOrdersByRoomAndTime(Integer roomId, LocalDateTime startTime);

    @Select("select count(*) from orders where room_id = #{roomId} and start_time <#{repairedTime} and end_time >=#{now}")
    boolean existsOrderBetween(Integer roomId, LocalDateTime now, LocalDateTime repairedTime);

    @Select("select * from orders where end_time<#{time}")
    List<Orders> selectByEndTime(LocalDateTime time);

    /**
     * 统计会议室未完成的预约数量
     * @param roomId 会议室ID
     * @return 未完成的预约数量
     */
    @Select("SELECT COUNT(*) FROM orders WHERE room_id = #{roomId} AND status IN (0, 1, 2)")
    int countActiveOrdersByRoomId(@Param("roomId") Integer roomId);
}